var jscookie = require('js-cookie')
var request = require('superagent')

import {
  LOAD,
  LOAD_SUCCESS,
  LOAD_FAIL,
  LOAD_WITH_TOKEN,
  LOGIN,
  LOGIN_SUCCESS,
  LOGIN_FAIL,
  LOGOUT,
  LOGOUT_SUCCESS,
  LOGOUT_FAIL,
  REGISTER,
  REGISTER_SUCCESS,
  REGISTER_FAIL
} from './constants';



const initialState = {
  loaded: false
};

export default function reducer(state = initialState, action = {}) {
  switch (action.type) {
    case LOAD:
      return {
        ...state,
        loading: true
      };
    case LOAD_SUCCESS:
      return {
        ...state,
        loading: false,
        loaded: true,
        user: action.result
      };
    case LOAD_FAIL:
      return {
        ...state,
        loading: false,
        loaded: false,
        error: action.error
      };
    case LOGIN:
      return {
        ...state,
        loggingIn: true
      };
    case LOGIN_SUCCESS:
      return {
        ...state,
        loggingIn: false,
        user: action.result
      };
    case LOGIN_FAIL:
      return {
        ...state,
        loggingIn: false,
        user: null,
        loginError: action.error
      };
    case LOGOUT:
      return {
        ...state,
        loggingOut: true
      };
    case LOGOUT_SUCCESS:
      return {
        ...state,
        loggingOut: false,
        user: null
      };
    case LOGOUT_FAIL:
      return {
        ...state,
        loggingOut: false,
        logoutError: action.error
      };
    case REGISTER_SUCCESS:
      return {
        ...state,
        loggingIn: false,
        user: action.result
      };

    default:
      return state;
  }
}

export function isLoaded(globalState) {
  return globalState.auth && globalState.auth.loaded;
}



export function login(name) {
  return {
    types: [LOGIN, LOGIN_SUCCESS, LOGIN_FAIL],
    promise: (client) => client.post('/login', {
      data: {
        name: name
      }
    })
  };
}

export function logout() {
  return {
    types: [LOGOUT, LOGOUT_SUCCESS, LOGOUT_FAIL],
    promise: (client) => client.get('/logout')
  };
}



// Customized function actions and non-function (standard) actions.

export function load() {
  return {
    types: [LOAD, LOAD_SUCCESS, LOAD_FAIL],
    promise: (client) => client.get('/user/load')
  };
}



export function logout_client_func_action() {
  return (dispatch) => {
    jscookie.remove('my_token')
    dispatch({type: LOGOUT_SUCCESS})
  }
}


export function login_func_action(email,password) {
  return (dispatch) => {
    request.post('/auth/local')
           .send({ "email": email, "password": password })
           .end(
                function(err, res){
                    // Calling the end function will send the request
                    
                    if (err) {
                      // if err.
                      console.log(err)
                    } else {
                      // if Login successfully.
                      console.log(res)
                      jscookie.set('my_token', res.body.token)

                      dispatch({result: res.body.username, type: LOGIN_SUCCESS})
                    }
                }
            )
  }
}


export function register_func_action(username, email, password) {
  return (dispatch) => {
    request.post('/api/user')
           .send({ "name": username, "email": email, "password": password })
           .end(
                function(err, res){
                    // Calling the end function will send the request
                    
                    if (err) {
                      // if err.
                      console.log(err)
                    } else {
                      // if Register successfully.
                      console.log(res)
                      jscookie.set('my_token', res.body.token)

                      dispatch({result: res.body.username, type: LOGIN_SUCCESS})
                    }
                }
            )
  }
}





